﻿using System;
using System.Web;
using FBGraph.Configuration;
using FBGraph.Web.Configuration;

namespace FBGraph.Web.Security
{
    /// <summary>An implementation of <see cref="IHttpHandlerFactory" /> that handles authentication requests for WebForms applications.</summary>
    public class FBGraphAuthenticationHandlerFactory : IHttpHandlerFactory
    {
        IHttpHandler IHttpHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated)
        {
            var config = new FBGraphConfigurationManager();
            var authRoot = VirtualPathUtility.MakeRelative(String.Empty, config.GetAuthUrl());
            var requestPath = context.Request.Url.AbsolutePath.Replace(authRoot, String.Empty);
            var requestPathRoot = requestPath.Split(new Char[] { '/' }, StringSplitOptions.RemoveEmptyEntries)[0];

            switch (requestPathRoot.ToUpper())
            {
                case FBGraphAuthorizeRequestHandler.PATH: return new FBGraphAuthorizeRequestHandler();
                case FBGraphPostAuthorizeRequestHandler.PATH: return new FBGraphPostAuthorizeRequestHandler();
                case FBGraphLogOutRequestHandler.PATH: return new FBGraphLogOutRequestHandler();

                default: return null;
            }
        }

        void IHttpHandlerFactory.ReleaseHandler(IHttpHandler handler)
        {
        }
    }
}